package com.music.mapper.music;

import com.music.dto.music.SongDTO;
import com.music.entity.collection.CollectionSongEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CollectionSongMapper {

    /*
     * @Description 收藏歌曲信息
     * @Author hbh
     * @Date 2021/12/5
     **/
    @Select({"<script>SELECT song_id, b.user_id, song_name, singer_name, album_name, song_info, " +
            "audio_url, audio_name, audio_size, user_name contributor, " +
            "(SELECT COUNT(*) count FROM music_download d LEFT JOIN music_song e ON e.song_id = d.song_id " +
            "WHERE a.song_id = e.song_id) downloadsNumber," +
            "(select collection_song_id from collection_song where song_id = a.song_id and user_id = #{userId}) " +
            "as 'key', " +
            "(select collection_song_id from collection_song where song_id = a.song_id and user_id = #{userId}) " +
            "collection_song_id " +
            "FROM music_song a LEFT JOIN music_playlist b ON a.playlist_id = b.playlist_id " +
            "LEFT JOIN basic_user c on b.user_id = c.user_id " +
            "WHERE a.is_public = 1 and b.is_public = 1 " +
            "and song_id IN (SELECT song_id FROM collection_song WHERE user_id = #{userId}) " +
            "<if test='songName != null'> AND song_name LIKE CONCAT('%', #{songName}, '%') </if>" +
            "<if test='singerName != null'> AND singer_name LIKE CONCAT('%', #{singerName}, '%') </if>" +
            "<if test='albumName != null'> AND album_name LIKE CONCAT('%', #{albumName}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "song_id ASC " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<SongDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("userId") Long userId,
            @Param("songName") String songName,
            @Param("singerName") String singerName,
            @Param("albumName") String albumName,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /*
     * @Description 收藏歌曲数量
     * @Author hbh
     * @Date 2021/12/5
     **/
    @Select({"<script>SELECT count(*) FROM collection_song " +
            "WHERE song_id IN (SELECT song_id FROM collection_song WHERE user_id = #{userId}) " +
            "<if test='songName != null'> AND song_name LIKE CONCAT('%', #{songName}, '%') </if>" +
            "<if test='singerName != null'> AND singer_name LIKE CONCAT('%', #{singerName}, '%') </if>" +
            "<if test='albumName != null'> AND album_name LIKE CONCAT('%', #{albumName}, '%') </if>" +
            "</script>"})
    Long count4Table(@Param("userId") Long playlistId,
                           @Param("songName") String songName,
                           @Param("singerName") String singerName,
                           @Param("albumName") String albumName) throws Exception;

    /*
     * @Description 新增收藏歌曲
     * @Author hbh
     * @Date 2021/12/5
     **/
    @Insert("Insert into collection_song ( " +
            "user_id, song_id, gmt_create) " +
            "VALUES (#{entity.userId}, #{entity.songId}, #{entity.gmtCreate})")
    Integer addCollectionSong(@Param("entity") CollectionSongEntity entity) throws Exception;

    /*
     * @Description 删除收藏歌曲
     * @Author hbh
     * @Date 2021/12/5
     **/
    @Delete("DELETE from collection_song WHERE collection_song_id = #{collectionSongId}")
    Integer remove(@Param("collectionSongId") Long collectionSongId) throws Exception;

    /*
     * @Description 删除收藏歌曲
     * @Author hbh
     * @Date 2021/12/5
     **/
    @Delete("Delete from collection_song where user_id = #{userId} and song_id = #{songId}")
    Integer removeByUserId(@Param("userId") Long userId,
                           @Param("songId") Long songId) throws Exception;
}
